package com.hmall.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.hmall.search.pojo.Item;
import com.hmall.search.pojo.ItemDoc;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 *
 */
@Component
public class RabbitMqImpl {

    @Autowired
    private RestHighLevelClient client;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.addQueue"),
            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
            key = {"item.add"}
    ))
    public void itemAdd(Item item) {
        try {
            item.setStatus(1);
            item.setSold(0);
            item.setCommentCount(0);
            //将数据转为文档类型
            ItemDoc itemDoc = new ItemDoc(item);
            //转json
            String json = JSON.toJSONString(itemDoc);
            //准备request对象
            IndexRequest request = new IndexRequest("hmall")
                    .id(itemDoc.getId().toString());
            request.source(json, XContentType.JSON);
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.deleteQueue"),
            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
            key = {"item.delete"}
    ))
    public void delete(String id){
        try {
            DeleteRequest request = new DeleteRequest("hmall", id);
            client.delete(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "item.updateQueue"),
//            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
//            key = {"item.update"}
//    ))
//    public void itemUpdate(Item item){
//        try {
//            ItemDoc itemDoc = new ItemDoc(item);
//            UpdateRequest request = new UpdateRequest("hmall", item.getId().toString());
//            request.doc(
//                "id",itemDoc.getId().toString(),
//                    "name",itemDoc.getName(),
//                    "price",itemDoc.getPrice().toString(),
//                    "stock",itemDoc.getStock().toString(),
//                    "image",itemDoc.getImage(),
//                    "brand",itemDoc.getBrand(),
//                    "spec",itemDoc.getSpec(),
//                    "sold",itemDoc.getSold().toString(),
//                    "commentCount",itemDoc.getCommentCount().toString(),
//                    "status",itemDoc.getStatus().toString(),
//                    "isAD",itemDoc.getIsAD().toString(),
//                    "updateTime",itemDoc.getUpdateTime().toString(),
//                    "suggestion",itemDoc.getSuggestion().toString()
//            );
//            client.update(request,RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }


//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "item.stateQueue"),
//            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
//            key = {"item.state"}
//    ))
//    public void updateState(Map map){
//        try {
//            String id = map.get("id").toString();
//            String state = map.get("state").toString();
//            UpdateRequest request = new UpdateRequest("hmall", id);
//            request.doc(
//                    "status",state
//            );
//            client.update(request,RequestOptions.DEFAULT);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
}
